package org.vectrics.recruiter.struts.tag.job;

import java.util.List;

import org.apache.log4j.Logger;
import org.vectrics.company.CompanyPerson;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.struts.tag.cv.PersonLanguagesViewTag;
import org.vectrics.struts.VectricsTagSupport;

/**
 * Tag que permite crear una nueva Entrevista para un usuario
 * 
 * @author pgoette
 */
public class AddCandidateTag extends VectricsTagSupport {

    private static final long serialVersionUID = -6966139307267042908L;

    static private transient Logger log = Logger.getLogger(PersonLanguagesViewTag.class);

    private String personId = "";

    private String companyId = "";

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(final String companyId) {
        this.companyId = companyId;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(final String personId) {
        this.personId = personId;
        log.debug("setPersonId() - set to" + personId);
    }

    @Override
    protected String makeStartTag() {

        log.debug("AddInterview.makeStartTag() called");
        String html = "";
        // Si el id es nulo muestro un mensaje de error
        if (getPersonId() == null) {
            html = "<div class='stdFont'> " + this.getPhrase("error.interview.add") + " </div>";
        } else {

            CompanyPerson employ = RecruitServiceLocator.getCompanyService().findEmployeeForIdPerson(
                    new Long(getPersonId()));

            if (employ != null)
                return html;

            // Recupero la lista de Jobs
            List<Job> jobs = RecruitServiceLocator.getJobProvider().findJobsForCandidate(new Long(getCompanyId()),
                    new Long(getPersonId()));
            if (jobs != null && !jobs.isEmpty()) {
                // Genero el formulario
                html += "<form method='POST' action='/hrs-intranet/admin/jobs/menu/candidate/add.do'>";
                // Creo la tabla
                html += " <table border=0> ";
                html += " <tr> ";
                html += " <td alingh='center' valingh='middle' > ";
                // Agrego el personId
                html += "<input type='hidden' name='personId' value='" + getPersonId() + "' />";
                html += "<input type='hidden' name='userId' value='" + getPersonId() + "' /> ";
                html += "<input type='hidden' name='companyId' value='" + getCompanyId() + "' /> ";
                // Creo el select que contiene los Jobs
                html += " <span class='subFont'>";
                html += this.getPhrase("prompt.interview.job");
                html += " </span>";
                html += " <select id='jobId' name='jobId' style=' width:280px; ' >";
                html += "<option value=''> </option>";

                for (Job job : jobs) {
                    html += "<option value='" + job.getId().toString() + "'> " + job.getReferenceId() + " - "
                            + job.getTitle() + "</option>";
                }
                html += " </select>";
                html += "</td> <td>";
                html += "<input type=\"image\" ";

                String caption = this.getPhrase("prompt.interview.candidate");
                String imageSource = getContextPath() + "/dynagraphic/button.jpg?code=button.param" + "&caption="
                        + caption;

                html += " src=\"";
                html += imageSource;
                html += "\" border=\"0\" ";
                html += "value=\"";
                html += "submit";
                html += "\" ";
                html += " name=\"";
                html += "submit";
                html += "\" ";
                html += " /> ";
                html += "</td>";
                html += "</tr>";
                html += "</table>";
                html += "</form>";
            } else {
                // Creo la tabla
                html += " <table border=0> ";
                html += " <tr> ";
                html += " <td alingh='center' valingh='middle' > ";
                // Creo el select que contiene los Jobs
                html += " <span class='subFont'>";
                html += this.getPhrase("prompt.interview.job");
                html += " </span>";
                html += " <select id='jobId' name='jobId' style=' width:280px; ' disabled='disabled' >";
                html += " </select>";
                html += "</td> <td>";
                html += "<input type=\"image\" ";
                String caption = this.getPhrase("prompt.interview.candidate");
                String imageSource = getContextPath() + "/dynagraphic/button.jpg?code=button.param" + "&caption="
                        + caption;
                html += " src=\"";
                html += imageSource;
                html += "\" border=\"0\" ";
                html += " /> ";
                html += "</td>";
                html += "</tr>";
                html += "</table>";
            }
        }
        return html;
    }

}
